package org.wechat4j.api.token;

import org.wechat4j.config.ApiConfig;
import org.wechat4j.exception.MpInvokeException;
import org.wechat4j.model.Response;
import org.wechat4j.utils.Https;
import org.wechat4j.utils.ResponseUtils;

/**
 * @author bjliuyong
 * @version 1.0
 * @created date 16-11-8
 */
public class AccessTokenUtils {

    /**
     * 获取AceessToken
     *
     * {"access_token":"ACCESS_TOKEN","expires_in":7200}
     * {"errcode":40013,"errmsg":"invalid appid hint: [YjBQPA0465e544]"}
     *
     * @param appId
     * @param appSecret
     * @return
     */
    public static String getAccessToken(String appId ,String appSecret) throws MpInvokeException{

        String accessTokenUrl = ApiConfig.baseUrl + "token?grant_type=client_credential&appid="
                + appId + "&secret=" + appSecret ;

        String content = Https.get(accessTokenUrl, null);
        return ((AccessToken) ResponseUtils.parse(content  ,AccessToken.class)).access_token;
    }

    static class AccessToken extends Response {
        public String access_token ;
        public int    expires_in   ;
    }
}
